﻿using ProjectFootball.MVC.Foundation;
using ProjectFootball.MVC.Foundation.Repositories;
using ProjectFootball.MVC.Models.EntityFramework.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace ProjectFootball.MVC.Models.RepositoryProviders
{
    public static class CountryRepositoryExtensions
    {
        public static IEnumerable<ICodePickerData> GetCodePickerData(this IDataRepository<Country> countryRepository)
        {
            return countryRepository
                .Where(c => c.IsApproved)
                .OrderBy(c => c.CountryName)
                .AsEnumerable()
                .Select(c =>
                    new GuidCodePicker()
                    {
                        Code = c.Pk,
                        Description = c.CountryName
                    });
        }

        public static IEnumerable<string> GetAutoCompleteList(this IDataRepository<Country> countryRepository, IPrincipal user, string searchText)
        {
            var countries = from c in countryRepository
                            where c.CountryName.Contains(searchText)
                            select c;

            return countries.AsEnumerable().Where(c => c.CanView(user)).Select(c => c.CountryName).Distinct().OrderBy(c => c);
        }

        public static Country SearchByPrimaryKey(this IDataRepository<Country> countryRepository, Guid id)
        {
            var countries = from c in countryRepository
                            where c.Pk == id
                            select c;

            return countries.Any() ? countries.Single() : null;
        }
    }
}